<?php
class Record_model extends CI_Model
{
	function __construct()
	{
		parent::__construct();
	}
	function getRecordData($data)
	{
		$this->db->select();
		if($data['record_time'] == 0)
		{
			$this->db->where('crea_time <',time()-(3*30*24*60*60) );
		}
		else
		{
			$this->db->where('crea_time >',time()-(3*30*24*60*60) );
		}
		$this->db->where('record_type',$data['record_type']);
		$this->db->where('user_id',$this->userInfo['id']);
		$this->db->from('record');
		$this->db->order_by('crea_time','desc');
		return $this->db->get()->result_array();
	}
	function getConsumeNum()
	{
		$this->db->select_sum('fund_info');
		$this->db->where('record_type',1);
		$this->db->where('user_id',$this->userInfo['id']);
		$data = $this->db->get('record')->result_array();
		return ($data[0]['fund_info'] == '')?0:$data[0]['fund_info'];
	}
	function getAddFinanceInfo()
	{
		$this->db->select();
		$this->db->where('user_id',$this->userInfo['id']);
		$data = $this->db->get('tb_order')->result_array();
		foreach($data as $k=>$v)
		{
			$data[$k]['out_time'] = date('Y-m-d H:i:s',$data[$k]['out_time']);
			$data[$k]['is_ok'] = ($data[$k]['is_ok'] == 0)?'金额锁定':'充值成功';
		}
		return $data;
	}
	function addRecord($recordType,$data,$userInfo=null)
	{
		switch ($recordType)
		{
			case 'addFinance'://充值
				$comment = ($data['is_ok'] == 0)?'因未确认收货，所以暂时被系统冻结，请尽快确认收货解冻使用。':'';
				$record_data = array(
									'user_id'=>$userInfo['id'],
									'record_type'=>0,
									'fund_info'=>$data['prince'],
									'score_info'=>0,
									'comment'=>'通过淘宝交易订单&lt;'.$data['order_number'].'&gt;成功充值金额￥'.$data['prince'].'。'.$comment.'充值后账户余额￥'.($this->userInfo['lock_fund']+$this->userInfo['fund']+$data['prince']).'。',
									'crea_time'=>time(),
				);
				break;
			case 'passFinance'://转账
				$record_data = array(
									'user_id'=>$data['post_user']['id'],
									'record_type'=>0,
									'fund_info'=>$data['pay_num'],
									'score_info'=>0,
									'comment'=>'收到&lt;'.$userInfo['user'].'&gt;账户的转账，转账前金额￥'.($data['post_user']['fund']*1+$data['post_user']['lock_fund']*1).'，转账后账户余额￥'.($data['post_user']['fund']*1+$data['post_user']['lock_fund']*1+$data['pay_num']*1).'。',
									'crea_time'=>time(),
				);
				break;
			case 'reFinance':
				if($data['shop_type'] == 0)//空包
				{
					$record_data = array(
									'user_id'=>$userInfo['id'],
									'record_type'=>0,
									'fund_info'=>0-$data['pay_num']*1,
									'score_info'=>0,
									'comment'=>'购买'.$data['order_num'].'个空包消费￥'.$data['pay_num'].'。消费后账户余额￥'.($userInfo['lock_fund']+$userInfo['fund']-$data['pay_num']).'。',
									'crea_time'=>time(),
					);
				}
				else if($data['shop_type'] == 1)//转账
				{
					$record_data = array(
									'user_id'=>$userInfo['id'],
									'record_type'=>0,
									'fund_info'=>0-$data['pay_num']*1,
									'score_info'=>0,
									'comment'=>'给'.$data['post_user']['user'].'账户转账，转账前金额￥'.($userInfo['lock_fund']+$userInfo['fund']).'，转账后账户余额￥'.($userInfo['lock_fund']+$userInfo['fund']-$data['pay_num']).'。',
									'crea_time'=>time(),
					);
				}
				else if($data['shop_type'] == 2)//申请底单
				{
					$record_data = array(
									'user_id'=>$userInfo['id'],
									'record_type'=>0,
									'fund_info'=>0-$data['pay_num']*1,
									'score_info'=>0,
									'comment'=>'申请'.$data['order_num'].'张快递单号底单消费￥'.$data['pay_num'].'。消费后账户余额￥'.($userInfo['lock_fund']+$userInfo['fund']-$data['pay_num']).'。',
									'crea_time'=>time(),
					);
				}
				break;
			case 'shoping'://消费记录
				$record_data = array(
								'user_id'=>$userInfo['id'],
								'record_type'=>1,
								'fund_info'=>$data['prince'],
								'score_info'=>0,
								'comment'=>$data['order_info'].'，运单号<font style="color:red;">&lt;'.$data['post_number'].'&gt;</font>',
								'crea_time'=>time(),
				);
				break;
		}
		$this->db->insert('record',$record_data);
	}
}